// pages/release/auction/auction.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aid: null,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    searchParams: {
      pageNum: 1,
      pageSize: 6,
      status: '1',
      keyword: '',
      pages: '',
      total: '',
    },
    nocar: true,
    carList: {},
    stepIndex: 1,
    scopeRow: {},
    scopePage: 1,
    scopeIndex: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  doNothing(){},
  onLoad: function (options) {
    this.InitUserStatus();
  },
  
  // 检查一下登录状态。
  async InitUserStatus() {
    app.showLoading("获取用户信息");
    await app.checkToken();
    this.InitUserInfo();
    wx.hideLoading();
  },
  InitUserInfo() {
    let userInfo = app.globalData.userInfo;
    this.setData({
      'userInfo': app.globalData.userInfo
    })
    // 这里再验证一遍权限。
    if (userInfo.roleList.indexOf('manageAuction') == -1) {
      app.showNoAuthOrNoGrant('您无权操作查看此页面');
      return;
    } else {
      this.carManageList(this.data.searchParams);
    }
  },

  goQuery() {
    this.setData({
      'searchParams.pageNum': 1,
      'searchParams.total': '',
      'searchParams.pages': '',
      'carList': {}
    });
    this.carManageList(this.data.searchParams);
  },
  carManageList(searchParams) {
    app.sHttp.carManageList(searchParams).then(res => {
      if (searchParams.pageNum == 1 && res.result.carList.length == 0) {
        this.setData({
          'nocar': true
        })
      } else {
        let carList = this.data.carList;
        carList[searchParams.pageNum] = res.result.carList;
        this.setData({
          'carList': carList,
          'nocar': false,
          'searchParams.pages': res.result.pages,
          'searchParams.total': res.result.total
        })
      }
    }, err => {
      app.error(err.msg);
    })
  },
  
  bindtapInput(e) {
    this.setData({
      'searchParams.keyword': e.detail.value
    })
  },
  onReachBottom() {
    if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
      //app.alert('没有更多了哦~');
    } else {
      let self = this;
      this.setData({
        'searchParams.pageNum': self.data.searchParams.pageNum + 1
      });
      this.carManageList(this.data.searchParams);
    }
  },
  chooseCar(e) {
    let page = e.currentTarget.dataset.page;
    let index = e.currentTarget.dataset.idx;
    let item = this.data.carList[page][index];
    app.globalData.scopeRowCar = item
    this.setData({
      'scopeRow': item,
      'scopePage': page,
      'scopeIndex': index
    });
  },
  BackPage() {
    var that = this;
    wx.navigateBack({
      delta: 1,
      fail: function () {
        that.toHome();
      }
    });
  },
  toHome() {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
})